<?php

namespace App\Http\Controllers;

use App\Competition;
use Faker\Generator as Faker;
use Illuminate\Support\Str;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;

class CompetitionController extends Controller
{

    public function __construct()
    {
        $this->middleware('auth:superadmin');
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request,Competition $competitions)
    {
        $count = collect([
            'all' => $competitions->count(),
            'status0' => $competitions->where('status', 0)->count(),
            'status1' => $competitions->where('status', 1)->count(),
            'status2' => $competitions->where('status', 2)->count(),
        ]);

        if (is_null($request->search)) {
            $competitions = is_null($request->type) ? $competitions->paginate(10) : $competitions->where('status', $request->type)->paginate(10);
        } else {
            $competitions = $competitions->where('title', 'like', "%{$request->search}%")
                ->orWhere('location', 'like', "%{$request->search}%")
                ->paginate(10);
        }
        return view('competitions.index', [
            'competitions' => $competitions,
            'all' => $count->get('all'),
            'status0' => $count->get('status0'),
            'status1' => $count->get('status1'),
            'status2' => $count->get('status2'),
        ]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('competitions.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request,Faker $faker)
    {
        $request->validate([
            'title' => 'required|min:3|max:255',
            'location' => 'required|min:3|max:255',
            'deadline' => 'required|date|after:tomorrow',
            'start_time' => 'required|date|after:deadline',
            'end_time' => 'required|date|after:start_time',
        ]);
        Competition::create([
            'title' => $request->title,
            // 'image' => Str::start(Str::finish($faker->numberBetween(1,500),'/200/200'),'https://picsum.photos/id/'),
            'image' => preg_replace('/^public\//', '/storage/', $request->imagefile->store('public/images')),
            'description' => $request->description,
            'location' => $request->location,
            'status' => 0,
            'start_time' => $request->start_time,
            'end_time' => $request->end_time,
            'deadline' => $request->deadline
        ]);

        $this->flash('比赛创建成功');
        return redirect('competitions');
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Competition  $competitions
     * @return \Illuminate\Http\Response
     */
    public function show(Competition $competitions)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Competition  $competitions
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $competitions = Competition::find($id);
        $time = collect([
            'deadline' => Carbon::parse($competitions->deadline)->toDateTimeLocalString(),
            'start_time' => Carbon::parse($competitions->start_time)->toDateTimeLocalString(),
            'end_time' => Carbon::parse($competitions->end_time)->toDateTimeLocalString(),
            'now' => Carbon::now()->toDateTimeLocalString()
        ]);

        return view('competitions.edit',[
            'competition' => $competitions,
            'time' => $time
        ]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Competition  $competitions
     * @return \Illuminate\Http\Response
     */
    public function update($id,Request $request)
    {
        $request->validate([
            'title' => 'required|min:6|max:255',
            'location' => 'required|min:3|max:255',
            'deadline' => 'required|date|after:tomorrow',
            'start_time' => 'required|date|after:deadline',
            'end_time' => 'required|date|after:start_time',
        ]);
        Competition::find($id)->update(request()->all());

        $this->flash('活动更新成功');
        return redirect('competitions');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Competition  $competitions
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $competitions = Competition::find($id);
        $competitions->status == 2 ? $competitions->status -= 1 : $competitions->status += 1;
        $competitions->save();

        $this->flash('更新比赛状态成功');
        return redirect('competitions');
    }

    function flash($message)
    {
        session()->flash('message', $message);
    }
}
